package com.cn.web.controller.sys;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.dto.DataStructureDto;
import com.cn.bean.dto.TestDto;
import com.cn.bean.pojo.mc.K3FlXx;
import com.cn.bean.pojo.sys.DataStructureMainTable;
import com.cn.bean.pojo.sys.DataStructureSub_Table;
import com.cn.bean.pojo.sys.Sys_Sequence_Info_T;
import com.cn.config.config.Result;
import com.cn.service.server.sys.DataStructureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@Slf4j
public class DataStructureController {
    @Autowired
    private DataStructureService dataStructureService;

    @RequestMapping("/select/dataStructure")
    @ResponseBody
    public List<DataStructureDto> selectDataStructure(@RequestBody DataStructureDto dataStructureDto) {
        List<DataStructureDto> assists = dataStructureService.selectDataStructure(dataStructureDto);
        return assists;
    }
    @RequestMapping("/select/dataAl")
    @ResponseBody
    public List<DataStructureDto> selectDataAl(@RequestBody DataStructureDto dataStructureDto) {
        List<DataStructureDto> assists = dataStructureService.selectAL(dataStructureDto);
        return assists;
    }
    @RequestMapping("/select/dataStructureSub")
    @ResponseBody
    public List<DataStructureDto> selectDataStructureSub(@RequestBody DataStructureDto dataStructureDto) {
        List<DataStructureDto> assists = dataStructureService.selectAll(dataStructureDto);
        return assists;
    }
    @RequestMapping("/insert/dataStructure")
    @ResponseBody
    public Result insertDataStructure(@RequestBody JSONObject jsonObject) {
//        DataStructureDto dataStructureDto = jsonObject.getObject("requestData", DataStructureDto.class);
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        DataStructureMainTable mainTable = new DataStructureMainTable();
        Sys_Sequence_Info_T sysSequenceInfoT=new Sys_Sequence_Info_T();
        Integer id = dataStructureService.getSequenceInfoMain();
        Integer mid=dataStructureService.getDataStructureAutoid();
        id = id != null ? ++id : 1;
        mid = mid != null ? ++mid : 1;
        sysSequenceInfoT.setId(id);
        sysSequenceInfoT.setTableId(mid);
        Boolean seq=dataStructureService.insertSequenceInfo(sysSequenceInfoT);
        String chineseName = jsonObject.getString("chineseName");
        String tableName = jsonObject.getString("tableName");
        mainTable.setId(mid);
        mainTable.setBelongingSubsystemId(id);
        mainTable.setChineseName(chineseName);
        mainTable.setTableName(tableName);
        if (seq){
            int a = dataStructureService.insertDataStructureMain(mainTable);
            if (a != 1) {
                return Result.failure("500","保存主表失败");
            }
            if (dataArray != null && !dataArray.isEmpty()) {
                List<DataStructureDto> djBhList = dataArray.toJavaList(DataStructureDto.class);
                for (DataStructureDto dataStructureDto : djBhList) {
                    DataStructureSub_Table subTable = new DataStructureSub_Table();
                    BeanUtils.copyProperties(dataStructureDto, subTable);
                    Integer autoid = dataStructureService.getDataStructureSubAutoid();
                    autoid = autoid != null ? ++autoid : 1;
                    subTable.setId(autoid);
                    subTable.setTableId(mid);
                    subTable.setSequenceNumber(autoid.toString());
                    int b = dataStructureService.insertDataStructureSub(subTable);
                    if (b != 1) {
                        return Result.failure("500","保存子表失败");
                    }
                }
            }
        }
        return Result.success(200);
    }
    @RequestMapping("/del/dataStructure")
    @ResponseBody
    public Result delDataStructure(@RequestBody DataStructureDto dataStructureDto) {
        int id=dataStructureDto.getId();
       List<DataStructureDto> dataStructureDtos=dataStructureService.selectSub(id);
        int c=dataStructureService.delDataStructureMain(id);
           int d=dataStructureService.delDataStructureSub(id);

       if (c!=1&&d!=1){
           return Result.failure("500","删除失败");
       }
        return Result.success(200);
    }
    @RequestMapping("/update/dataStructure")
    @ResponseBody
    public Result updateDataStructure(@RequestBody JSONObject jsonObject) {
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        DataStructureMainTable mainTable = new DataStructureMainTable();
        int id = jsonObject.getInteger("id");

        String chineseName = jsonObject.getString("chineseName");
        int belongingSubsystemId = jsonObject.getInteger("belongingSubsystemId");
        String tableName = jsonObject.getString("tableName");
        String functionDescription= jsonObject.getString("functionDescription");
        mainTable.setId(id);
        mainTable.setChineseName(chineseName);
        mainTable.setFunctionDescription(functionDescription);
        mainTable.setBelongingSubsystemId(belongingSubsystemId);
        mainTable.setTableName(tableName);
        List<DataStructureDto> dataStructureDtos=dataStructureService.selectSub(id);
        if (dataStructureDtos.size()>0){
            int sub = dataStructureService.delDataStructureSub(id);
        }
        Boolean seq=dataStructureService.delSequenceInfo(id);
        if (seq){
            int a = dataStructureService.updateMain(mainTable);
            if (a != 1) {
                return Result.failure("500","更新主表失败");
            }
        }
        if (dataArray==null||dataArray.isEmpty()){
            return Result.success(200);
        }
        List<DataStructureSub_Table> djBhList = dataArray.toJavaList(DataStructureSub_Table.class);
        for (DataStructureSub_Table dataStructureDto : djBhList) {
            DataStructureSub_Table subTable = new DataStructureSub_Table();
            BeanUtils.copyProperties(dataStructureDto, subTable);
            Integer autoid = dataStructureService.getDataStructureSubAutoid();
            autoid = autoid != null ? ++autoid : 1;
            subTable.setId(autoid);
            subTable.setTableId(id);
            subTable.setSequenceNumber(autoid.toString());
            int b = dataStructureService.insertDataStructureSub(subTable);
            if (b != 1) {
                return Result.failure("500","更新子表失败");
            }
        }
        return Result.success(200);
    }
}
